package deibay.server.frontend;

import org.jdom.Document;
import org.jdom.Element;

import deibay.XmlIO;
import deibay.network.ServerAddress;

public class XmlServerAddress {

	private Document doc = null;
	private Element root = null;
	
	public XmlServerAddress()
	{
		loadXml();
	}
	
	private void createXml()
	{	
		doc = new Document();
		root = new Element("Server");
		doc.setRootElement(root);		
	}
	
	private void loadXml()
	{		
		doc = XmlIO.readXML("ServerAddress.xml");
		if(doc == null)
		{
			createXml();
			XmlIO.writeXmltoFile(doc, "ServerAddress.xml");
			doc = XmlIO.readXML("ServerAddress.xml");
		}
		root = doc.getRootElement();			
	}
	
	public ServerAddress getServerAddress()
	{
		ServerAddress server = null;
		String ip = root.getChild("ip").getValue();
		int port = Integer.parseInt(root.getChild("port").getValue());
		server = new ServerAddress(ip, port);
		return server;
	}
}
